package com.infore.quartz.main.controller;

import com.infore.common.response.ApiEntityResponse;
import com.infore.common.response.ApiResponse;
import com.infore.quartz.main.model.QuartzJob;
import com.infore.quartz.main.utils.ScheduleUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/quartz")
@Api(tags = "定时器管理")
public class QuartzController {

    @Autowired
    private Scheduler smartScheduler;

    @PostMapping("/createJob")
    @ApiOperation(value = "创建定时任务", notes = "创建定时任务")
    public ApiResponse createJob(@RequestBody QuartzJob quartzJob){
        try {
            String jobName = URLDecoder.decode(quartzJob.getJobDetailName(),"UTF-8");
            String jobGroup = URLDecoder.decode(quartzJob.getGroupName(),"UTF-8");
            String jobDescription = URLDecoder.decode(quartzJob.getJobDescription(),"UTF-8");
            ScheduleUtils.createJob(smartScheduler,quartzJob.getJobCronExpression(),jobName,jobGroup,jobDescription,Class.forName(quartzJob.getJobClassName()));
        } catch (Exception e) {
            log.error("格式不支持",e);
        }
        return ApiResponse.ok();
    }

    @PostMapping("/updateJob")
    @ApiOperation(value = "修改定时任务", notes = "修改消息分发定时任务")
    public ApiResponse rescheduleDispatchJob(@RequestBody QuartzJob quartzJob){
        String jobName="";
        String jobGroup="";
        try {
            jobName = URLDecoder.decode(quartzJob.getJobDetailName(),"UTF-8");
            jobGroup = URLDecoder.decode(quartzJob.getGroupName(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("格式不支持",e);
        }
        ScheduleUtils.updateJob(smartScheduler,quartzJob.getJobCronExpression(),jobName,jobGroup);
        return ApiResponse.ok();
    }


    @PostMapping(value="/pauseJob")
    @ApiOperation(value = "暂停定时任务", notes = "暂停定时任务")
    public ApiResponse pauseJob(@RequestBody QuartzJob quartzJob)
    {
        String jobName="";
        String jobGroup="";
        try {
            jobName = URLDecoder.decode(quartzJob.getJobDetailName(),"UTF-8");
            jobGroup = URLDecoder.decode(quartzJob.getGroupName(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("格式不支持",e);
        }
        ScheduleUtils.pauseJob(smartScheduler,jobName, jobGroup);
        return ApiResponse.ok();
    }

    @PostMapping(value="/delJob")
    @ApiOperation(value = "删除定时任务", notes = "删除定时任务")
    public ApiResponse delJob(@RequestParam(name = "jobDetailName") String jobDetailName,@RequestParam(name = "groupName") String groupName){
        String decodeJobName = "";
        String decodeJobGroup = "";
        try {
            decodeJobName = URLDecoder.decode(jobDetailName,"UTF-8");
            decodeJobGroup = URLDecoder.decode(groupName,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("格式不支持",e);
        }
        ScheduleUtils.delJob(smartScheduler,decodeJobName, decodeJobGroup);
        return ApiResponse.ok();
    }



    @PostMapping(value="/resumeJob")
    @ApiOperation(value = "恢复定时任务", notes = "恢复定时任务")
    public ApiResponse resumeJob(@RequestBody QuartzJob quartzJob)
    {
        String jobName="";
        String jobGroup="";
        try {
            jobName = URLDecoder.decode(quartzJob.getJobDetailName(),"UTF-8");
            jobGroup = URLDecoder.decode(quartzJob.getGroupName(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("格式不支持",e);
        }
        ScheduleUtils.resumeJob(smartScheduler,jobName, jobGroup);
        return ApiResponse.ok();
    }


    @GetMapping(value="/getAllJobs")
    @ApiOperation(value = "获取所有的定时任务", notes = "获取所有的定时任务")
    public ApiEntityResponse<Map<String,Object>> getAllJobs()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> allJobs = ScheduleUtils.getAllJobs(smartScheduler);
        map.put("jobs", allJobs);
        if(allJobs!=null){
            map.put("number", allJobs.size());
        }else{
            map.put("number",0);
        }
        return ApiEntityResponse.ok(map);
    }

}
